/**
 *
 */
package com.evil.Servlet;

import com.evil.Utils.Log;
import com.evil.Utils.Utils;
import com.mysql.fabric.xmlrpc.base.Array;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

/******************************************
 * 工程名：EdaSupport<br>
 * 包名：com.evil.Servlet<br>
 * 时间：2016年7月8日 下午9:04:00<br>
 * 作者：白雨濃 evil<br>
 * 邮箱：evilbai@foxmail.com<br>
 * 内容：<br>
 ******************************************/
public class Version extends HttpServlet
{
    private HttpServletRequest request;
    private HttpServletResponse response;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        this.request = request;
        this.response = response;

        String type = request.getParameter("type");
        String versionCode = request.getParameter("versionCode");

        if (type == null)
            newuser();
        else
        {
            if (type.equals("new"))
                newuser();
            else if (type.equals("update"))
                update(versionCode);
        }

        PrintWriter out = response.getWriter();
        out.flush();
        out.close();
    }

    /**
     * 新用户使用
     *
     * @throws IOException
     */
    private void newuser() throws IOException
    {
        String path = request.getSession().getServletContext().getRealPath("/");//获取路径
        File rootDir = new File(path + "/update");
        String[] fileList = rootDir.list();

        for (String s : fileList)
            Log.println(s);

        if (fileList != null)
            Arrays.sort(fileList);

        String filename = null;
        if (fileList != null)
            filename = fileList[fileList.length - 1];

        response.sendRedirect("./update/" + filename);
    }

    /**
     * 升级使用
     *
     * @throws IOException
     */
    private void update(String versionCode) throws IOException
    {
        String path = request.getSession().getServletContext().getRealPath("/");//获取路径
        File rootDir = new File(path + "/update");
        String[] fileList = rootDir.list();

        if (fileList != null)
            Arrays.sort(fileList);

        String filename = null;
        if (fileList != null)
            filename = fileList[fileList.length - 1];

        int code = Utils.toInt(versionCode);
        int serverCode = 0;
        if (filename != null)
        {
            serverCode = Utils.toInt(filename
                    .substring(filename.indexOf("code")).replace("code", "")
                    .replace(".apk", ""));
        }

        PrintWriter out = response.getWriter();

        if (code >= serverCode)
            out.print("false");
        else if (code < serverCode)
            out.print(request.getRequestURL().toString()
                    .replace("Version", "update/" + filename));
        out.flush();
        out.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        this.doGet(request, response);
    }

}
